package com.lwww.infinitechat.authenticationservice.utils;

import cn.hutool.core.util.StrUtil;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;

import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class OSSUtils {
    @Resource
    private MinioClient minioClient;

    @Value("${minio.url}")
    private String url;

    public String uploadUrl(String bucketName, String objectName, Integer expires) throws Exception{
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.PUT) //限制的不是调用这个Java函数的对象，而是限制之后使用这个预签名URL的客户端（例如浏览器、移动App等）必须使用PUT请求方法来访问这个URL
                        .bucket(bucketName)
                        .object(objectName)
                        .expiry(expires, TimeUnit.SECONDS)
                        .build());
    }

    public String downloadUrl(String bucketName, String fileName) throws Exception{
        return url+ StrUtil.SLASH + bucketName + StrUtil.SLASH + fileName;
    }
}
